#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class TopicPublisher01 : public rclcpp::Node
{
public:
    TopicPublisher01(std::string name) : Node(name){
        RCLCPP_INFO(this->get_logger(),"%s节点已经启动",name.c_str());
        RCLCPP_INFO(this->get_logger(),"大家好,我是%s.",name.c_str());
        //创建发布者
        command_publisher_ = this->create_publisher<std_msgs::msg::String>("command",10);
        //创建定时器,500mszhouqi,定时发bu(用点拼音证明不是cv大法)
        timer_ = this->create_wall_timer(std::chrono::milliseconds(500),
                                         std::bind(&TopicPublisher01::timer_callback,this));
    }
private:
    void timer_callback(){
        std_msgs::msg::String message; //创建消息
        message.data = "forward";
        //日志打印
        RCLCPP_INFO(this->get_logger(),"publishing: '%s'",message.data.c_str());
        //发布消息
        command_publisher_->publish(message);
    }
    //声明定时器指针
    rclcpp::TimerBase::SharedPtr timer_;
    //声明话题发布者
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_;
};

int main(int argc,char **argv){
    rclcpp::init(argc,argv);

    //创建对应节点的共享指针对象
    auto node = std::make_shared<TopicPublisher01>("topic_publisher_01");

    //运行节点，检测退出信号
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}
